package com.csc.bankingsystem.web.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csc.bankingsystem.core.dto.AccountInfoDTO;
import com.csc.bankingsystem.core.dto.UserInfoDTO;
import com.csc.bankingsystem.core.entity.AccountInfo;
import com.csc.bankingsystem.core.enumeration.AccountInfoStateEnum;
import com.csc.bankingsystem.core.service.AccountInfoService;
import com.csc.bankingsystem.web.util.CaptchaServiceSingleton;
import com.octo.captcha.service.CaptchaServiceException;

/**
 * Handles requests for the captcha request
 * @author angelkid
 */
@Controller
public class CaptchaLoginController {
	
	private static final Logger logger = LoggerFactory.getLogger(CaptchaLoginController.class);
	
	private static final String TAG = "CaptchaLoginController";
	
	@RequestMapping("/jcaptcha.jpg")
	public ResponseEntity<byte[]> getCaptcha(HttpServletRequest request) throws IOException {
		byte[] captchaChallengeAsJpeg = null;
		// the output stream to render the captcha image as jpeg into
		ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
		try {
			// get the session id that will identify the generated captcha.
			// the same id must be used to validate the response, the session id
			// is a good candidate!
			String captchaId = request.getSession().getId();
			// call the ImageCaptchaService getChallenge method
			BufferedImage challenge = CaptchaServiceSingleton.getInstance()
					.getImageChallengeForID(captchaId,
							request.getLocale());

			// a jpeg encoder
			ImageIO.write(challenge, "jpeg" , jpegOutputStream);
		} catch (IllegalArgumentException e) {
			logger.info(TAG,"IllegalArgumentException in function getCaptcha");
		} catch (CaptchaServiceException e) {
			logger.info(TAG,"CaptchaServiceException in function getCaptcha");
		}
		
		//Convert Outputstream into Byte[]
		captchaChallengeAsJpeg = jpegOutputStream.toByteArray();

		final HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.IMAGE_JPEG);

		//Return Byte[] to client
		return new ResponseEntity<byte[]>(captchaChallengeAsJpeg, headers,
				HttpStatus.CREATED);
	}
}
